#include <iostream>
#include <unistd.h>     // dup2()
#include <sys/types.h>  // open(file_name, int flag)
#include <sys/stat.h>
#include <fcntl.h>
#include <string.h>
#include <sys/wait.h>   // waitpid()

using namespace std;

const char* file_name = "log.txt";

void out_test()
{
    int fd = open(file_name, O_WRONLY|O_CREAT|O_TRUNC, 0666);
    dup2(fd, 1);    // redirect from screen(stdout) to file
    printf("fd_vocal_JiaRenMen\n", fd);
}

void in_test()
{
    int fd = open(file_name, O_RDONLY, 0666);
    dup2(fd, 0);
    char buffer[1024];
    scanf("%s", buffer);
    printf("%s\n", buffer);
}

int main()
{
    pid_t id = fork();
    if(id == 0)
    {
        out_test();     // redirect write to file
    }
    pid_t rid = waitpid(id, NULL, 0);
    if(rid == id)
    {
        printf("Wait Success...\n");
        in_test();
    }
    return 0;
}

